package com.ymttest.database.mongo;

import java.util.Date;
import java.util.List;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.WriteResult;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.mongo.query.FirstPaidOrder;
import com.ymttest.database.mongo.config.FirstPaidOrderField;
import com.ymttest.database.mongo.config.MongoYmtBuyerOrdersConfig;
import com.ymttest.database.mongo.config.SchemaField;
import com.ymttest.utils.YMTDateUtil;

/**
 * 
 *用户行为分析mongo 查询
 */
public class UserAnalysisMongoWapper {

	private static final String BUYER_SELLER_ORDER_STATISTICS = "BuyerSellerOrderStatistics";
	private static final String FIRST_PAID_ORDER = "FirstPaidOrder";
	private static final String ORDER_COUNT_SUMMARY = "BuyerOrderCount";

	private MongoTemplate mongoOrderTemplate;

	public UserAnalysisMongoWapper() {
		try {
			mongoOrderTemplate = new MongoYmtBuyerOrdersConfig()
					.mongoTemplate();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public MongoTemplate getMongoOrderTemplate() {
		return mongoOrderTemplate;
	}

	/**
	 * 根据订单号获取订单详细信息
	 *
	 * @param buyerId
	 * @return
	 */
	public FirstPaidOrder findById(Integer buyerId) {

		return mongoOrderTemplate.findById(buyerId, FirstPaidOrder.class,
				SchemaField.FIRST_PAID_ORDER);

	}

	public WriteResult removeBuyerOrder(Integer buyerId) {
		return mongoOrderTemplate.remove(
				new Query(Criteria.where(FirstPaidOrderField.ID).is(buyerId)),
				FirstPaidOrder.class, SchemaField.FIRST_PAID_ORDER);
	}

	/**
	 * 根据订单号获取订单详细信息
	 *
	 * @param id
	 * @return
	 */
	public BuyerSellerOrderStatisticsMongoBean findById(long id) {

		return mongoOrderTemplate.findById(id,
				BuyerSellerOrderStatisticsMongoBean.class,
				BUYER_SELLER_ORDER_STATISTICS);
	}

	/**
	 * 根据订单号获取订单详细信息
	 *
	 * @param id
	 * @return
	 */
	public WriteResult updateFirstPaidTime(long id, Date newTime,
			boolean isNoneOrAllCancelled) {

		Query query = new Query(Criteria.where("_id").is(id));

		Update update = new Update().set("FirstPaidTime", newTime).set(
				"NoneOrAllCancelled", isNoneOrAllCancelled);

		return mongoOrderTemplate.updateFirst(query, update,
				BUYER_SELLER_ORDER_STATISTICS);

	}

	/**
	 * 根据ID获取订单数量汇总信息
	 *
	 * @param userId
	 * @return
	 */
	public OrderCountSummaryBean findOrderCountSummaryById(long userId) {

		Query query = Query.query(Criteria.where("PayCount").gt(3));

		return mongoOrderTemplate.findById(userId, OrderCountSummaryBean.class,
				ORDER_COUNT_SUMMARY);
	}

	/**
	 * 根据ID获取订单数量汇总信息
	 *
	 * @param userIds
	 * @return
	 */
	public List<OrderCountSummaryBean> findOrderCountSummarysByIds(
			List<Long> userIds) {

		Query query = Query.query(Criteria.where("_id").in(userIds));
		return mongoOrderTemplate.find(query, OrderCountSummaryBean.class,
				ORDER_COUNT_SUMMARY);
	}
	/**
	 * 查询vip用户 limit 100
	 * @param sellerid
	 * @return
	 */
	public List<BuyerSellerOrderStatisticsMongoBean> findVipBySellerid(
			Long sellerid) {
		Query query = Query.query(Criteria.where("SellerId").in(sellerid).andOperator(Criteria.where("FirstPaidTime").lte(YMTDateUtil.getDateBeforeOrNextSecond(-5*60*60*24+300)))).limit(100);
		return mongoOrderTemplate.find(query, BuyerSellerOrderStatisticsMongoBean.class,
				BUYER_SELLER_ORDER_STATISTICS);
	}
	public static void main(String args[]) {
		try {
			Logger.info(""
					+ new UserAnalysisMongoWapper()
							.findVipBySellerid(24635724L).get(0).getUserId());
//			Logger.info(""
//					+ new UserAnalysisMongoWapper()
//							.findById(86924550318591287L).getFirstPaidTime());
//
//			new UserAnalysisMongoWapper().updateFirstPaidTime(
//					86924550318591287L, new Date(), false);
//
//			Logger.info(""
//					+ new UserAnalysisMongoWapper()
//							.findById(86924550318591287L).getFirstPaidTime());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
